
import { baseUrl } from './config.js';


// 获取所有榜单
export function topList(){
	return new Promise(function(resolve,reject){
		uni.request({
			url: `${baseUrl}/toplist/detail`,
			method: 'GET',
			data: {},
			success: res => {
				let result = res.data.list;
				resolve(result.splice(0,4));
			},
			fail: (err) => {
				console.log(err);
			},
			complete: () => {}
		});
	});
}

// 获取歌单详情
export const getPlaylistDetail = (id) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseUrl}/playlist/detail`,  // 使用统一 baseUrl
      method: 'GET',
      data: { id },
      success: (res) => {
        // 适配常见网易云API响应结构
        if (res.data.code === 200) {
          resolve(res.data.playlist); // 直接返回 playlist 数据
        } else {
          reject(new Error(res.data.msg || '获取数据失败'));
        }
      },
      fail: (err) => {
        reject(new Error('网络请求失败'));
      }
    });
  });
};
// 获取歌曲详情
export const getSongDetail = (id) => {
  return uni.request({
    url: 'http://localhost:3000/song/detail',
    data: { ids: id }
  });
};

// 获取歌曲URL
export const getSongUrl = (id) => {
  return uni.request({
    url: 'http://localhost:3000/song/url', 
    data: { id }
  });
};
// 获取歌词
export const getLyric = (songId) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: 'http://localhost:3000/lyric', // 替换为实际歌词接口
      method: 'GET',
      data: { id: songId },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data); // 直接返回 API 响应数据
        } else {
          reject(new Error('歌词请求失败'));
        }
      },
      fail: (err) => reject(err)
    });
  });
};
// 获取相似歌曲
export const getSimiSong = (id) => {
  return request(`/simi/song?id=${id}`)
}
// 获取歌曲评论
export const getMusicComment = (id) => {
  return request(`/comment/music?id=${id}`)
}
// 获取热门搜索
export const getSearchHotDetail = () => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseUrl}/search/hot/detail`,
      success: (res) => {
        if (res.data.code === 200) {
          resolve({
            data: res.data.data.map(item => ({
              searchWord: item.searchWord,
              score: item.score
            }))
          })
        } else {
          reject(new Error('热门搜索获取失败'))
        }
      },
      fail: (err) => reject(err)
    })
  })
}
// 获取搜索建议
export const getSearchSuggest = (keywords) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseUrl}/search/suggest`,
      method: 'GET',
      data: { keywords },
      success: (res) => {
        if (res.data.code === 200) {
          resolve({
            data: {
              result: {
                // 网易云实际返回结构
                allMatch: res.data.result.allMatch || [],
                songs: res.data.result.songs || []
              }
            }
          })
        } else {
          reject(new Error(res.data.msg || '搜索建议获取失败'))
        }
      },
      fail: (err) => reject(err)
    })
  })
}
export function searchWord(word){
	return uni.request({
		url : `${baseUrl}/search?keywords=${word}`,
		method : 'GET'
	})
}
export default {
  getPlaylistDetail,
  getSongDetail,
  getSongUrl,
  getLyric,
  getSimiSong,
  getMusicComment,
  getSearchHotDetail,
  getSearchSuggest
}